package cn.easybuy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.easybuy.entity.Order;
import cn.easybuy.entity.OrderList;
import cn.easybuy.entity.Page;
import cn.easybuy.service.OrderService;
import cn.easybuy.service.impl.OrderServiceImpl;

import com.alibaba.fastjson.JSON;

@WebServlet("/allOrder")
public class OrderServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String opr = req.getParameter("opr");
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		OrderService os = new OrderServiceImpl();
		if ("init".equals(opr)){
			try {
				Map<Order,List<OrderList>> orders = os.findOrder();
				Map<String,List<OrderList>> o = new LinkedHashMap<String, List<OrderList>>();
				Set<Order> set = orders.keySet();
				for (Order s : set) {
					String key = "loginName:"+s.getLoginName()+"|userAddress:"+s.getUserAddress()+"|serialNumber:"+
							s.getSerialNumber()+"|cost:"+s.getCost()+"|id:"+s.getId();
					o.put(key, orders.get(s));
				}
				String json = JSON.toJSONString(o);
				out.print(json);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if ("initUser".equals(opr)){
			try {
				List<Order> list = os.findUser();
				String json = JSON.toJSONString(list);
				out.print(json);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if ("myOrder".equals(opr)){
			String loginName = (String)req.getSession().getAttribute("userName");
			System.out.println("loginName:"+loginName);
			String sta = req.getParameter("sta");
			int status = 0;
			if(sta != null){
				status = Integer.valueOf(sta);
			}
			try {
				Map<Order,List<OrderList>> orders = os.findOrderStatus(loginName, status);
				Map<String,List<OrderList>> o = new LinkedHashMap<String, List<OrderList>>();
				Set<Order> set = orders.keySet();
				for (Order s : set) {
					String key = "loginName:"+s.getLoginName()+"|userAddress:"+s.getUserAddress()+"|serialNumber:"+
							s.getSerialNumber()+"|cost:"+s.getCost()+"|id:"+s.getId();
					o.put(key, orders.get(s));
				}
				String json = JSON.toJSONString(o);
				out.print(json);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if ("userSelect".equals(opr)){
			String loginName = req.getParameter("loginName");
			String sta = req.getParameter("status");
			int status = 0;
			if(sta != null){
				status = Integer.valueOf(sta);
			}
			try {
				Map<Order, List<OrderList>> orders = os.findOrderStatus(loginName, status);
				Map<String,List<OrderList>> o = new LinkedHashMap<String, List<OrderList>>();
				Set<Order> set = orders.keySet();
				for (Order s : set) {
					String key = "loginName:"+s.getLoginName()+"|userAddress:"+s.getUserAddress()+"|serialNumber:"+
							s.getSerialNumber()+"|cost:"+s.getCost()+"|id:"+s.getId();
					o.put(key, orders.get(s));
				}
				String json = JSON.toJSONString(o);
				out.print(json);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if ("findLike".equals(opr)){
			String sn = req.getParameter("sn");
			String name = req.getParameter("loginName");
			String sta = req.getParameter("status");
			int status = 0;
			if(sta != null){
				status = Integer.valueOf(sta);
			}
			try {
				Map<Order, List<OrderList>> orders = os.findLike(sn,name,status);
				Map<String,List<OrderList>> o = new LinkedHashMap<String, List<OrderList>>();
				Set<Order> set = orders.keySet();
				for (Order s : set) {
					String key = "loginName:"+s.getLoginName()+"|userAddress:"+s.getUserAddress()+"|serialNumber:"+
							s.getSerialNumber()+"|cost:"+s.getCost()+"|id:"+s.getId();
					o.put(key, orders.get(s));
				}
				String json = JSON.toJSONString(o);
				out.print(json);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if ("findTypeStatus".equals(opr)){
			String sta = req.getParameter("status");
			int status = 0;
			if(sta != null){
				status = Integer.valueOf(sta);
			}
			try {
				Map<Order, List<OrderList>> orders = os.findOrderStatus(null, status);
				Map<String,List<OrderList>> o = new LinkedHashMap<String, List<OrderList>>();
				Set<Order> set = orders.keySet();
				for (Order s : set) {
					String key = "loginName:"+s.getLoginName()+"|userAddress:"+s.getUserAddress()+"|serialNumber:"+
							s.getSerialNumber()+"|cost:"+s.getCost()+"|id:"+s.getId();
					o.put(key, orders.get(s));
				}
				String json = JSON.toJSONString(o);
				out.print(json);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		out.flush();
		out.close();
	}
	
}
